package com.wmlive.hhvideo.heihei.subject;

import android.content.Intent;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.wmlive.hhvideo.application.DCApplication;

import cn.wmlive.hhvideo.R;

import com.wmlive.hhvideo.common.base.DcBaseActivity;
import com.wmlive.hhvideo.heihei.beans.subject.TopicCreateResponse;
import com.wmlive.hhvideo.heihei.subject.View.SubjectAddView;
import com.wmlive.hhvideo.heihei.subject.presenter.SubjectAddPresenter;
import com.wmlive.hhvideo.utils.StringUtils;
import com.wmlive.hhvideo.utils.ToastUtil;

import butterknife.BindView;

/**
 * 添加话题部分
 */
public class SubjectAddActivity extends DcBaseActivity<SubjectAddPresenter> implements SubjectAddView {
    public static final String SUBJECT_TITLE_FLAG = "SUBJECT_TITLE_FLAG";

    @BindView(R.id.et_subject_add_content)
    EditText et_subject_add_content;
    @BindView(R.id.et_subject_add_description)
    EditText et_subject_add_description;
    @BindView(R.id.tv_subject_limit_count)
    TextView tv_subject_limit_count;
    @BindView(R.id.btn_subject_add_submit)
    Button btn_subject_add_submit;

    private int iMaxSubjectContentLength = 28;
    private int iMaxSubjectDesLength = 120;

    private SubjectAddPresenter subjectAddPresenter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_subject_add;
    }

    @Override
    protected SubjectAddPresenter getPresenter() {
        if (subjectAddPresenter == null) {
            subjectAddPresenter = new SubjectAddPresenter(this);
        }
        return subjectAddPresenter;
    }

    @Override
    protected void initData() {
        super.initData();
        setTitle("添加爆点", true);
        //话题的内容
        InputFilter[] filters = new InputFilter[1];
        filters[0] = new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                int sourceLen = StringUtils.getLengthOfByteCode(source.toString());
                int destLen = StringUtils.getLengthOfByteCode(dest.toString());
                if ((sourceLen + destLen > 0) && !checkSubjectDesEmpty()) {
                    btn_subject_add_submit.setEnabled(true);
                }
                if (dstart == 0 && StringUtils.isBlackChar(source.toString()))// 第一个不能为空格
                {
                    return "";
                }

                if (dest.toString().endsWith(" ") && StringUtils.isBlackChar(source.toString()))// 不能连续两个空格
                {
                    return "";
                }

                if (dstart >= 1 && dstart <= dest.length()) {
                    String sectionStr = dest.subSequence(dstart - 1, dstart).toString();
                    if (StringUtils.isBlackChar(sectionStr) && StringUtils.isBlackChar(source.toString())) {
                        return "";
                    }
                }


                if (sourceLen + destLen > iMaxSubjectContentLength)// 不能超过28个字节
                {
                    ToastUtil.showToast(R.string.subject_add_error);
                    try {
                        if (source.length() > iMaxSubjectContentLength) {
                            return source.subSequence(0, iMaxSubjectContentLength);
                        }
                    } catch (Exception e) {

                    }
                    return "";
                }
                return source;
            }
        };
        et_subject_add_content.setFilters(filters);

        InputFilter[] filtersDes = new InputFilter[1];
        filtersDes[0] = new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                int sourceLen = StringUtils.getLengthOfByteCode(source.toString());
                int destLen = StringUtils.getLengthOfByteCode(dest.toString());
                if ((sourceLen + destLen > 0) && !checkSubjectContentEmpty()) {
                    btn_subject_add_submit.setEnabled(true);
                }

                if (dstart == 0 && StringUtils.isBlackChar(source.toString()))// 第一个不能为空格
                {
                    return "";
                }

                if (dest.toString().endsWith(" ") && StringUtils.isBlackChar(source.toString()))// 不能连续两个空格
                {
                    return "";
                }

                if (dstart >= 1 && dstart <= dest.length()) {
                    String sectionStr = dest.subSequence(dstart - 1, dstart).toString();
                    if (StringUtils.isBlackChar(sectionStr) && StringUtils.isBlackChar(source.toString())) {
                        return "";
                    }
                }


                if (sourceLen + destLen > iMaxSubjectDesLength)// 不能超过120个字节
                {
                    ToastUtil.showToast(R.string.subject_add_error);
                    try {
                        if (source.length() > iMaxSubjectDesLength) {
                            return source.subSequence(0, iMaxSubjectDesLength);
                        }
                    } catch (Exception e) {

                    }

                    return "";
                }
                return source;
            }
        };
        et_subject_add_description.setFilters(filtersDes);

        btn_subject_add_submit.setOnClickListener(this);

        String strSubjectTitle = getIntent().getStringExtra(SUBJECT_TITLE_FLAG);
        et_subject_add_content.setText(strSubjectTitle);
    }

    @Override
    public void topicCreateSuccess(TopicCreateResponse response) {
        //添加成功
        Intent intent = new Intent();
        intent.putExtra("topic", response.getTopic());
        setResult(SubjectSearchActivity.REQUEST_CODE_FLAG, intent);
        finish();
    }

    @Override
    public void handleNetError() {
        //添加失败
        ToastUtil.showToast(R.string.net_error);
    }

    @Override
    protected void onSingleClick(View v) {
        switch (v.getId()) {
            case R.id.btn_subject_add_submit:
                //提交话题
                submitSubject();
                break;
        }

    }

    /**
     * 提交话题
     */
    public void submitSubject() {
        if (checkSubjectContentEmpty()) {
            ToastUtil.showToast(R.string.subject_add_content_empty);
            return;
        }
        if (checkSubjectDesEmpty()) {
            ToastUtil.showToast(R.string.subject_add_des_empty);
            return;
        }
        subjectAddPresenter.submitSubjectInfo(et_subject_add_content.getText().toString().trim(), et_subject_add_description.getText().toString().trim());
    }

    /**
     * 显示剩余数量
     *
     * @param count
     */
    public void showSubjectLimitCount(int count) {
        if (count < 0) {
            tv_subject_limit_count.setText(getText(R.string.subject_limit_count_content));
        } else {
            tv_subject_limit_count.setText(DCApplication.getDCApp().getResources().getString(R.string.subject_surplus_count_content, String.valueOf(count)));
        }
    }

    /**
     * 检查话题描述
     *
     * @return
     */
    public boolean checkSubjectDesEmpty() {
        String strDes = et_subject_add_description.getText().toString().trim();
        return TextUtils.isEmpty(strDes);
    }

    /**
     * 检查话题内容
     *
     * @return
     */
    public boolean checkSubjectContentEmpty() {
        String strContent = et_subject_add_content.getText().toString().trim();
        return TextUtils.isEmpty(strContent);
    }

}
